package com.sqkj.controller;

import com.sqkj.entity.Article;
import com.sqkj.service.ArticleService;
import com.sqkj.service.UserService;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/article")
@RestController
public class ArticleController {


    @Resource
    ArticleService service;
    @Resource
    UserService userService;

    @RequestMapping("/add")
    public Map<String, Object> add(String title, String authorName, String content) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("isSuccess", false);
        Boolean bo = userService.UserCount(authorName);


        String dateStr = UpLoadController.dateformat.format(System.currentTimeMillis());
        map.put("isSuccess", bo);
        if (bo == true) {
            Article article = new Article(title, userService.findByName(authorName).get(0).getId(), 0, dateStr, content, 0, 0, 0, 0, "", "", "", "");
            service.add(article);
            return map;
        } else {
            return map2;
        }


    }

    @RequestMapping("/findById")
    public Map<String, Object> findById(int id) {
        Map<String, Object> map = new HashMap<String, Object>();
        Article article1 = service.findById(id);
        service.addViewCount(article1.getArticleViewCount() + 1, id);
        Article article2 = service.findById(id);
        article2.setAuthorId(article2.getUser().getId());
        String tags = article2.getTag();
         if (tags.lastIndexOf(",") ==-1) {
             if (tags.isEmpty()&& "".equals(tags)) {
                 map.put("Article", article2);
                 return map;
             }
             map.put("tags,", tags);
        } else {
            String[] tag = tags.split(",");
            map.put("tags", tag);
        }

        map.put("Article", article2);
        return map;

    }

    @RequestMapping("/findAll")
    public Map<String, Object> findAll() {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Article> list = service.findAll();
        for (Article article : list) {
            article.setUser(userService.findById(article.getAuthorId()));
        }
        map.put("Articles", list);
        return map;
    }

    @RequestMapping("/deleteById")
    public Map<String, Object> deleteById(int id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("isSuccess", service.deleteById(id));
        return map;
    }

    @RequestMapping("/randFind")
    public Map<String, Object> rand() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("results", service.randFind());
        return map;
    }

    @RequestMapping("/findArticleByAuthorId")
    public Map<String, Object> UserArticle(int authorId) {
        Map<String, Object> map = new HashMap<>();
        List<Article> UserArticles = service.findAllByAuthorId(authorId);
        for (Article article : UserArticles) {
            article.setAuthorId(article.getUser().getId());
        }
        if (UserArticles == null && UserArticles.size() <= 0) {
            map.put("UserArticles", "暂无文章");
            return map;
        } else {
            map.put("UserArticles", UserArticles);
            return map;
        }
    }

    @RequestMapping("/addGoodCnt")
    public void addGoodCount(int id) {
        int GoodCount = service.findById(id).getArticleGoodCnt();
        service.addGoodCut(GoodCount + 1, id);
    }

    @RequestMapping("/reduceGoodCnt")
    public void reduceGoodCount(int id) {
        int GoodCount = service.findById(id).getArticleGoodCnt();
        service.reduceGoodCut(GoodCount - 1, id);
    }

}
